<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controllerform library
jimport('joomla.application.component.controllerform');
 
/**
 * HelloWorld Controller
 */
class ShopControllerProduct extends JControllerForm
{

	public function save()
	{
		$user		= JFactory::getUser();
		$db =& JFactory::getDBO();
		JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');
		$row = JTable::getInstance('Product','ShopTable');
		$row->reorder();
		$row->bind(JRequest::get('post'));
		
		///--- insert new params
		// Set alias
		$vtitle = JRequest::getVar('vtitle','');
		$etitle = JRequest::getVar('etitle','');
		$alias = JRequest::getVar('alias','');
		$alias = JApplication::stringURLSafe($alias);
		if (empty($alias)) {
			$alias = JApplication::stringURLSafe($title);
		}
		$row->alias = $alias;
				
		$row->ordering  = 1;
		$subadd = JRequest::getVar( 'subadd', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$row->subadd = $subadd;
		$introtext = JRequest::getVar( 'introtext', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$row->introtext = $introtext;
		$fulltext = JRequest::getVar( 'fulltext', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$row->fulltext = $fulltext;
		$row->image = JRequest::getVar('mainimage','');
		
	 	//-------------- CHECK UPLOAD FILE ----------
		include  JPATH_COMPONENT_ADMINISTRATOR . DS . "class.upload.php";
		$img_path = JPATH_SITE . DS . 'images' . DS . 'logo_image' .DS;
		
		$img = new Upload($_FILES['upload_file']);
		$new_img_name = $this->name_id_element($row->alias.rand(1,9));
		
		$uploaded = false;
		if ($img->uploaded) {	  					
			//--- upload image origin
			$img->image_convert = 'jpg';
			$img->file_new_name_body = $new_img_name;
			$img->Process($img_path);
			
			// resize image thumb
			$img->file_new_name_body = $new_img_name;
			$img->image_resize = true;
			$img->image_convert = 'jpg';
			$img->image_x = 160;
			$img->image_ratio_y = true;
			$img->Process($img_path.'thumb'.DS);
			if ($img->processed) {
				$row->logo_img = $new_img_name.'.jpg';
				$uploaded = true;
			}
		}
		$img = JRequest::getVar('image');
		if($uploaded == true && $img != ''){
			$this->deleteFile($img_path,$img);
			$this->deleteFile($img_path.'thumb'.DS,$img);
		}
		//-------------- end upload file  -----------
		
		if($row->store())
		{
		  $imageattach    = JRequest::getVar('imageattach', array(), '', 'array');
			$id_attach = implode(',',$imageattach);
			
			$query = $db->getQuery(true);
			$query->update('#__shop_attach');
			$query->set('product_id = '.$row->id);
			$query->where('id in ('.$id_attach.')');
			$db->setQuery((string)$query);
			$db->query(); 
		 	
			$this->setMessage(JText::_('COM_SHOP_PRODUCT_SAVE_SUCCESS'));
			$this->setRedirect('index.php?option=com_shop&view=products');
		}
		else
		{
			$this->setMessage(JText::_('COM_SHOP_PRODUCT_SAVE_FAIL'));
			$this->setRedirect('index.php?option=com_shop&view=products');
		}
	}
	public function getNumberInString($text) {
            $string = preg_replace('/[^0-9]/', '', $text);
        	return $string;
    }
	public function name_id_element($string = null) {
    	$string = trim($string);
    	$string = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $string);
    	$string = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $string);
    	$string = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $string);
    	$string = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $string);
    	$string = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $string);
    	$string = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $string);
    	$string = preg_replace("/(đ)/", 'd', $string);

    	$string = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $string);
    	$string = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $string);
    	$string = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $string);
    	$string = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $string);
    	$string = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $string);
    	$string = preg_replace("/(\'|\")/", '', $string);
    	$string = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $string);
    	$string = preg_replace("/(Đ)/", 'D', $string);
    	$string = str_replace("--", '_', $string);
    	$string = str_replace("--", '_', $string);
    	$string = str_replace("--", '_', $string);
        $string = str_replace("-", '_', $string);

        $string = str_replace(" ", '_', $string);
         $string = str_replace("/", '_', $string);
    	return $string;
    }
}
